package com.etc.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.etc.dao.UserMapper;
import com.etc.entity.User;
import com.etc.service.UserService;
import com.etc.util.MD5Util;
import com.etc.util.PageData;

@Service
public class UserServiceImpl implements UserService {

	@Resource
	private UserMapper um;

	@Override
	public boolean deleteByPrimaryKey(Integer userid) {
		// TODO Auto-generated method stub
		return um.deleteByPrimaryKey(userid) > 0;
	}

	@Override
	public boolean insert(User record) {
		// TODO Auto-generated method stub
		return um.insert(record) > 0;
	}

	@Override
	public boolean insertSelective(User record) {
		// TODO Auto-generated method stub
		return um.insertSelective(record) > 0;
	}

	@Override
	public User selectByPrimaryKey(Integer userid) {
		// TODO Auto-generated method stub
		return um.selectByPrimaryKey(userid);
	}

	@Override
	public boolean updateByPrimaryKeySelective(User record) {
		// TODO Auto-generated method stub
		return um.updateByPrimaryKeySelective(record) > 0;
	}

	@Override
	public boolean updateByPrimaryKey(User record) {
		// TODO Auto-generated method stub
		return um.updateByPrimaryKey(record) > 0;
	}

	@Override
	public PageData<User> selectByPage(int page, int limit, String content) {
		// 得到起始位置
		int start = (page - 1) * limit;
		// 得到记录总条数
		int count = um.selectCount(content);
		// 得到分页和模糊查询后的数据
		List<User> list = um.selectByPage(start, limit, content);
		PageData<User> pg = new PageData<>(list, count, limit, page, content);
		return pg;
	}

	// 管理员登录校验
	@Override
	public boolean selectByUser(User user) {
		User newuser = um.selectByUser(user);
		if (newuser != null && newuser.getLevel() == 0) {
			return true;
		}
		return false;
	}

	// 根据用户名校验
	@Override
	public boolean selectByUsername(String username) {
		User user = um.selectByUsername(username);
		if (user != null) {
			return true;
		}
		return false;
	}

	// 普通用户登录校验
	@Override
	public User checkByUser(User user) {
		User User = um.checkByUser(user);
		// if (User != null) {
		// return true;
		// }
		return User;
	}

	// 用户注册
	@Override
	public boolean insertuser(User user) {
		if (user.getStatus() == null) {
			user.setStatus(1);
		}
		if (user.getLevel() == null) {
			user.setLevel(1);
		}

//		if(user.getBirthday()==null) {
//			user.setBirthday(new Date().toLocaleString());
//		}
		if(user.getSex()==null) {
			user.setSex("男");
		}
		String pwd = MD5Util.getEncodeByMd5(user.getUserpwd());
		user.setUserpwd(pwd);
		return um.insertuser(user) > 0;
	}

	@Override
	public boolean updateStauts(int userid) {
		// TODO Auto-generated method stub
		return um.updateStauts(userid) > 0;
	}
}
